<?php
	namespace Home\Controller;
	// use Think\Controller;
	class BuycarController extends CommonController{  
		public function index(){
             //顶部判断，用户是否登录
            $username = session('user');
            
            $this->assign('username',$username['username']);
            //实例化 buycar类
            $buycar = D('buycar'); 
            //调用buycar类方法 查询相对应购物车数据
            $list = $buycar->selc();
            //分配数据
            $this->assign('list',$list['list']);
            $this->assign('total',$list['total']);
              //调用分类列表
            $this->type_secl();
            //调用友情链接
            $this->link_s();
            //调用导航栏
            $this->bar();
            //显示模板
            $this->display(); 
		}
        //详情页加入购物车
        public function writebc(){
            $buycar = D('buycar');

            $res = $buycar->addbc(); 
            echo $res;
        }
        //删除
        public function del_bc(){ 
            //实例化buycar表
            //测试session
            $uid = session('user')['id'];
            $bc = D('buycar');
            $res = $bc->del_buycar();
            // dump($res);
            // exit;
            if($res){
                // // redirect("/zhuxian/TP/index.php/Home/Buycar/index.html?uid={$uid}", 1, '删除成功');
                // $this->success('删除成功', "/zhuxian/TP/index.php/Home/Buycar/index.html?uid={$uid}", 1);
                echo '删除成功';
            }else{
                // redirect("/zhuxian/TP/index.php/Home/Buycar/index.html?uid={$uid}", 1, '删除失败');
                // $this->error('删除失败',"/zhuxian/TP/index.php/Home/Buycar/index.html?uid={$uid}", 1);
                echo '删除失败'; 
            }
            
        }
        //减
        public function up_bc(){
            $bc = D('buycar');
            $res = $bc->up_buycar();
            echo $res;
        }
        //加
        public function up_bct(){
            $bc = D('buycar');
            $res = $bc->up_buycart();
            echo $res;
        }
        //清空购物车
        public function clear_bc(){
            $bc = D('buycar');
            $res = $bc->clear_bcd();
             if($res){
                 echo "清空购物车成功";
                 // $this->success('清空购物车成功', "/zhuxian/TP/index.php/Home/Buycar/index.html?uid={$uid}", 1);
            }else{
                echo "清空购物车失败";
                // $this->error('清空购物车失败',"/zhuxian/TP/index.php/Home/Buycar/index.html?uid={$uid}", 1);
            }
        }
        //查询执行删除后总价格
        public function sec_total(){
            $bc = D('buycar');
            $total = $bc->total_sec(); 
            echo $total;
        }
        // 查询是否选中方法
        public function by_status(){
            $bc = D('buycar');
            $res = $bc->status_sec();
            echo $res;
        }
        //列表页加入购物车
        public function list_buy(){
            //获取商品id
            $gid = I('get.id');
            // 获取用户id
            $uid = session('user')['id'];
            // 实例化norm表
            $norm = M('norms'); 
            //实例化goods表
            $goods = M('goods');
            //查询商品信息
            // 设置查询条件
            $map['id'] = ['eq',$gid];
            $glist = $goods->where($map)->find();
            $map = [];
            $map['gid'] = ['eq',$gid];
            $nlist = $norm->where($map)->find();
            // 取得自己所需要的数据
            $data = array();
            $data['gid'] = $gid;
            $data['uid'] = $uid;
            $data['goodsname'] = $glist['goods'];
            $data['goodspic'] = $glist['pic'];
            $data['size'] = $nlist['size'];
            $data['color'] = $nlist['color'];
            $data['price'] = $glist['price'];
            $data['number'] = 1;
            $data['integral'] = $glist['num'];
            $data['total'] = $glist['price'];
            //写进购物车表
            // 实例化购物车表
            $buycar = M('buycar');
            $res = $buycar->add($data);
            if($res){
                $this->success('加入购物车成功',U('Home/buycar/index'),1);
            }else{
                $this->error('加入购物车失败');
            }

        }
          //首页分类查询方法
        public function type_secl(){
           $type = D('type');
           $t_list = $type->type_sec();
           $this->assign('t_list',$t_list);
        }
        //首页友链查询方法
        public function link_s(){
            $links = D('links');
            $link = $links->links_sec();
            // dump($list);
            $this->assign('link',$link);
        }
        //输出导航栏
        public function bar(){
            $type = D('type');
            $t_bar = $type->bar_sec();
            $this->assign('bar',$t_bar);
        }
	}